/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aburakc.sender;

import aburakc.MainMIDlet;
import aburakc.deliver.SMSDeliver;
import aburakc.message.ConfirmMessage;
import aburakc.message.SMSTextMessage;
import java.io.IOException;
import java.util.Vector;
import javax.wireless.messaging.Message;
import javax.wireless.messaging.MessageConnection;
import javax.wireless.messaging.TextMessage;

/**
 *
 * @author burak.costu
 */
public class SMSSender implements Runnable {

    private Vector smsVector = new Vector();
    private Message message;
    private Thread thread;
    private static SMSSender instance;
    SMSTextMessage sMSTextMessage;
    private boolean destroy = false;
    private MainMIDlet mainMIDlet ;
    private SMSSender() {
    }

    public static void createInstance(MainMIDlet mIDlet){
        instance = new SMSSender();
        instance.setMainMIDlet(mIDlet);
        instance.setThread(new Thread(instance));
    }

    public static SMSSender getInstance() {
        return instance;
    }


    public void run() {
        while (!destroy) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                //ex.printStackTrace();
            }
            while(smsVector.size() > 0){
                try {
                    SMSTextMessage sms = (SMSTextMessage) smsVector.firstElement();
                    String address = sms.getAddress() + ":" + getMainMIDlet().getSmsPort();
                    TextMessage txtmessage = (TextMessage) getMainMIDlet().getMessageConnection().newMessage(MessageConnection.TEXT_MESSAGE);
                    txtmessage.setAddress("sms://"+address);
                    txtmessage.setPayloadText(sms.getMessage());
                    getMainMIDlet().getMessageConnection().send(txtmessage);
                    ConfirmMessage cm = new ConfirmMessage();
                    cm.setMessageId(sms.getMessageId());
                    SMSDeliver.getInstance().addMesssage(cm);
                    smsVector.removeElement(sms);
                } catch (IOException ex) {
                    ex.printStackTrace();
                } 
           }
            

        }

    }

    public void sendSms(SMSTextMessage message) {
        smsVector.addElement(message);
        if(!thread.isAlive())
            thread.start();
    }

    /**
     * @return the message
     */
    public Message getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(Message message) {
        this.message = message;
    }

    /**
     * @return the mainMIDlet
     */
    public MainMIDlet getMainMIDlet() {
        return mainMIDlet;
    }

    /**
     * @param mainMIDlet the mainMIDlet to set
     */
    public void setMainMIDlet(MainMIDlet mainMIDlet) {
        this.mainMIDlet = mainMIDlet;
    }

    /**
     * @return the thread
     */
    public Thread getThread() {
        return thread;
    }

    /**
     * @param thread the thread to set
     */
    public void setThread(Thread thread) {
        this.thread = thread;
    }
}
